#include "wtlpch.h"

#include "TARConfigs.h"

CTARConfigs::CTARConfigs()
{
}

CTARConfigs::~CTARConfigs()
{
}

HRESULT CTARConfigs::LoadConfigFromXML()
{
	HRESULT hr = S_OK;

	//Get App Settings
	hr |= AppSettings.LoadSettings();

	//Get Jsharer Settings
	hr |= JSSettings.LoadSettings();

	return hr;
}

HRESULT CTARConfigs::LoadBoolValue(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, bool& bValue)
{
	//Prepare the buffer
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };
	hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	if(::wcscmp(lpwstrConfigData, CONFIG_TRUE_STRING) == 0)
	{
		bValue = true;
	}
	else
	{
		bValue = false;
	}

	return hr;
}

HRESULT CTARConfigs::LoadStringValueW(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, wstring& wstrValue, bool bIsCDATA)
{
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };

	if(bIsCDATA)
	{
		hr = pPhaser->getNextCDATA(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	}
	else
	{
		hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	}

	wstrValue = lpwstrConfigData;

	return hr;
}

HRESULT CTARConfigs::LoadStringValueA(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, string& strValue, bool bIsCDATA)
{
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };
	CHAR lpstrConfigDataA[MAX_XML_CONF_CDATA_CHARS*2] = { 0 };

	if(bIsCDATA)
	{
		hr = pPhaser->getNextCDATA(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	}
	else
	{
		hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	}

	::WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, lpwstrConfigData, -1, lpstrConfigDataA, MAX_XML_CONF_CDATA_CHARS*2, NULL, NULL);

	strValue = lpstrConfigDataA;

	return hr;
}

HRESULT CTARConfigs::LoadLongValue(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, LONG& lValue)
{
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };

	hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);

	lValue = ::_wtol(lpwstrConfigData);

	return hr;
}

HRESULT CTARConfigs::LoadUINTValue(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, UINT& uiValue)
{
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };

	hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
	
	uiValue = (UINT)::_wtoi(lpwstrConfigData);

	return hr;
}

HRESULT CTARConfigs::LoadStringVectorW(CLiteXMLPhaser* pPhaser, LPCWSTR lpcwstrPath, LPCWSTR lpcwstrPropery, vector<wstring>& vecwstrValue, bool bIsCDATA)
{
	HRESULT hr;
	WCHAR lpwstrConfigData[MAX_XML_CONF_CDATA_CHARS] = { 0 };

	if(bIsCDATA)
	{
		hr = pPhaser->getNextCDATA(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
		while(hr == S_OK)
		{
			vecwstrValue.push_back(wstring(lpwstrConfigData));
			::wmemset(lpwstrConfigData, 0, MAX_XML_CONF_CDATA_CHARS);

			hr = pPhaser->getNextCDATA(lpcwstrPath, lpcwstrPropery, lpwstrConfigData, true);
		}	
	}
	else
	{
		hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData);
		while(hr == S_OK)
		{
			vecwstrValue.push_back(wstring(lpwstrConfigData));
			::wmemset(lpwstrConfigData, 0, MAX_XML_CONF_CDATA_CHARS);

			hr = pPhaser->getNextText(lpcwstrPath, lpcwstrPropery, lpwstrConfigData, true);
		}	
	}

	return hr;
}